package sb.simple.schedule.way_service;

import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * 使用ScheduledExecutorService创建定时任务
 */
public class ExecutorServiceTask {
    public static void main(String[] args) {
        runTask();
    }

    private static int count = 0;

    private static void runTask() {
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                System.out.println("count is:" + count++);
            }
        };

        ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
        service.scheduleWithFixedDelay(task, 0, 1000, TimeUnit.MILLISECONDS);
    }
}
